package com.touzbi.hss.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import com.touzbi.hibernate.util.HibernateUtil;
import com.touzbi.hss.beans.Patient;
import com.touzbi.hss.bo.AbstractPatientListBO;
import com.touzbi.hss.bo.PatientListBO;
import com.touzbi.hss.dao.AbstractDAOFactory;
import com.touzbi.hss.dao.DAOFactory;
import com.touzbi.hss.pagemanager.AbstractPageManager;
import com.touzbi.hss.pagemanager.HttpPageManager;
import com.touzbi.hss.parametermanager.AbstractParameterManager;
import com.touzbi.hss.parametermanager.HttpParameterManager;

/**
 * Servlet implementation class Patientservlet
 */
@WebServlet("/patient-list")
public class PatientListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final String PATIENT_JSP = "/WEB-INF/list_patient.jsp";

	private static final String LIST_PATIENT_NAMES_ATT = "list";
	private static final String LIST_TITLE_ATT = "title";
	private static final String PAGE_INDEX_ATT = "page";
	private static final String HEADER_TITLES_ATT = "titles";
	private static final String PAGE_COUNT_ATT = "pageCount";
	private static final String LIST_TYPE_ATT = "listType";
	private static final String PATIENT_LIST_BO_ATT = "bo";

	private static final String LIST_TITLE = "List Patient";
	private static final String[] HEADER_TITLES = { "First name", "Last name",
			"Mail", "Phone number" };

	private static final String LIST_TYPE = "patient-list";
	private static final int PATIENT_LIST_PAGE_SIZE = 13;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		AbstractDAOFactory daoFactory = new DAOFactory(
				HibernateUtil.getSessionFactory());
		AbstractParameterManager paramManager = new HttpParameterManager(req);
		AbstractPageManager pageManager = new HttpPageManager(
				PATIENT_LIST_PAGE_SIZE, paramManager);

		Session session = daoFactory.getSession();
		session.beginTransaction();

		AbstractPatientListBO patientListBO = new PatientListBO(daoFactory,
				pageManager, req);

		List<Patient> listPatient = patientListBO.getPatientList();

		int pageCount = pageManager.getPageCount((int) patientListBO
				.getPatientCount());

		session.getTransaction().commit();

		req.setAttribute(LIST_PATIENT_NAMES_ATT, listPatient);
		req.setAttribute(LIST_TITLE_ATT, LIST_TITLE);
		req.setAttribute(PATIENT_LIST_BO_ATT, patientListBO);
		req.setAttribute(LIST_TYPE_ATT, LIST_TYPE);
		req.setAttribute(HEADER_TITLES_ATT, HEADER_TITLES);
		req.setAttribute(PAGE_COUNT_ATT, pageCount);
		req.setAttribute(PAGE_INDEX_ATT,
				pageManager.translatePageIndex(pageManager.getPageNumber()));

		this.getServletContext().getRequestDispatcher(PATIENT_JSP)
				.forward(req, resp);
	}
}
